package database

import (
	"fmt"
	"wanner_service/utils"
)

type User struct {
	Id       string `gorm:""`
	Username string
	Password string
}

func (User) TableName() string {
	return "tb_users"
}

func GetUser() (int, []User) {
	var users []User
	db.Find(&users)
	fmt.Println(users)

	return 200, users
}

func AddUser(user User) int {
	user.Id = utils.GetMD5(user.Username)
	user.Password = utils.GetMD5(user.Password)
	res := db.Create(user)
	if res.Error != nil {
		return utils.ERROR_ADD_USER
	}
	return utils.SUCCESS_ADD_USER
}

func (u User) CheckPassword() (bool, int) {
	var userInDB User
	//根据用户名查出密码
	db.Raw("select * from public.tb_users where username = ?", u.Username).Scan(&userInDB)
	//fmt.Println(userInDB)
	if utils.GetMD5(u.Password) == userInDB.Password {
		return true, utils.SUCCESS_CHECK_PASSWORD
	}
	return false, utils.ERROR_CHECK_PASSWORD
}
